package com.mashibing.service.impl;

import com.mashibing.internalcommon.constant.CommonStatusEnum;
import com.mashibing.internalcommon.constant.DriverCarConstants;
import com.mashibing.internalcommon.constant.IdentityConstants;
import com.mashibing.internalcommon.dto.DriverCarBindingRelationship;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.remote.DriverUserClient;
import com.mashibing.internalcommon.remote.ServiceOrderClient;
import com.mashibing.internalcommon.request.DriverGrabRequest;
import com.mashibing.internalcommon.request.DriverUserWorkStatus;
import com.mashibing.internalcommon.request.OrderRequest;
import com.mashibing.internalcommon.response.OrderDriverResponse;

import com.mashibing.service.ApiDriverOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApiDriverOrderServiceImpl implements ApiDriverOrderService {

    @Autowired
    private ServiceOrderClient serviceOrderClient;

    @Autowired
    private DriverUserClient driverUserClient;

    @Override
    public ResponseResult toPickUpPassenger(OrderRequest orderRequest) {
        return serviceOrderClient.changeStatus(orderRequest);
    }

    @Override
    public ResponseResult arrivedDeparture(OrderRequest orderRequest) {
        return serviceOrderClient.arrivedDeparture(orderRequest);
    }

    @Override
    public ResponseResult pickUpPassenger(OrderRequest orderRequest) {
        return serviceOrderClient.pickUpPassenger(orderRequest);
    }

    @Override
    public ResponseResult passengerGetOff(OrderRequest orderRequest) {
        return serviceOrderClient.passengerGetOff(orderRequest);
    }

    @Override
    public ResponseResult cancel(Long orderId, String identity) {
        return serviceOrderClient.cancel(orderId, IdentityConstants.DRIVER_IDENTITY);
    }

    @Override
    public ResponseResult grab(String driverPhone, Long orderId, String receiveOrderCarLongitude, String receiveOrderCarLatitude) {
        // 根据 司机的电话，查询车辆信息
        ResponseResult<DriverCarBindingRelationship> driverCarRelationShipResponseResult = driverUserClient.getDriverCarRelationShip(driverPhone);

        if (driverCarRelationShipResponseResult == null){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getMessage(),null);
        }
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarRelationShipResponseResult.getData();
        Long carId = driverCarBindingRelationship.getCarId();

        ResponseResult<OrderDriverResponse> availableDriverResponseResult = driverUserClient.getDriverUserInfoByCarId(carId.toString());
        if (availableDriverResponseResult == null || availableDriverResponseResult.getData() ==null){
            return ResponseResult.fail(CommonStatusEnum.CAR_NOT_EXISTS.getCode(),CommonStatusEnum.CAR_NOT_EXISTS.getMessage(),null);
        }

        OrderDriverResponse orderDriverResponse = availableDriverResponseResult.getData();
        Long driverId = orderDriverResponse.getDriverId();
        String licenseId = orderDriverResponse.getLicenseId();
        String vehicleNo = orderDriverResponse.getVehicleNo();
        String vehicleType = orderDriverResponse.getVehicleType();

//        orderInfo.setOrderStatus(OrderConstants.DRIVER_RECEIVE_ORDER);
        // 执行抢单动作
        DriverGrabRequest driverGrabRequest = new DriverGrabRequest();
        driverGrabRequest.setOrderId(orderId);
        driverGrabRequest.setDriverId(driverId);
        driverGrabRequest.setCarId(carId);
        driverGrabRequest.setDriverPhone(driverPhone);
        driverGrabRequest.setLicenseId(licenseId);
        driverGrabRequest.setVehicleNo(vehicleNo);
        driverGrabRequest.setVehicleType(vehicleType);
        driverGrabRequest.setReceiveOrderCarLatitude(receiveOrderCarLatitude);
        driverGrabRequest.setReceiveOrderCarLongitude(receiveOrderCarLongitude);

        ResponseResult responseResult = serviceOrderClient.driverGrab(driverGrabRequest);
        if (responseResult.getCode() != CommonStatusEnum.SUCCESS.getCode()){
            return ResponseResult.fail(CommonStatusEnum.ORDER_UPDATE_ERROR.getCode(),CommonStatusEnum.ORDER_UPDATE_ERROR.getMessage(),null);
        }

        // 修改司机的工作状态
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverId);
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_SERVING);

        responseResult = driverUserClient.updateStatus(driverUserWorkStatus);
        if (responseResult.getCode() != CommonStatusEnum.SUCCESS.getCode()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_STATUS_UPDATE_ERROR.getCode(),CommonStatusEnum.DRIVER_STATUS_UPDATE_ERROR.getMessage(),null);
        }

        return ResponseResult.success();
    }
}
